package com.ahs.algorithm;

import org.junit.jupiter.api.Test;

import java.util.Arrays;

/**
 * @author riven zhang
 * @since 2021/9/1 14:15
 * <p>
 * 删除有序数组中的重复选项
 */
public class RemoveDuplicate {

    @Test
    void test() {
        int[] arr = {2, 2, 2, 3, 4, 4, 5, 6, 7, 8, 9, 9};
        System.out.println(Arrays.toString(arr));
        int i = removeDuplicate(arr);
        System.out.println(Arrays.toString(arr));
    }

    private int removeDuplicate(int[] arr) {
        int n = 0;
        for (int i = 1; i < arr.length; i++) {
            if (arr[n] != arr[i]) {
                n++;
                arr[n] = arr[i];
            }
        }
        return n + 1;
    }
}
